﻿using System.Globalization;
using System.Linq.Expressions;
using System.Net;
using System.Text.RegularExpressions;

namespace dpa.Library.Helper; 

public static class StringProcessStaticMethod 
{
    /// <summary>
    /// 空格去除\s
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string ReplaceBlank(this string s) => Regex.Replace(s, @"\s", "", RegexOptions.Compiled).Trim();

    /// <summary>
    /// 多余空格去除\s
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string ReplaceRedundantBlank(this string s) => Regex.Replace(s, @"\s{2,}", " ", RegexOptions.Compiled).Trim();

    /// <summary>
    /// 日期格式化
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    public static string FormatDate(this string date)
    {
        DateTime.TryParse(date, CultureInfo.CreateSpecificCulture("en-US"), out var dateTime);
        return dateTime.ToString("MM/dd/yyyy");
    }

    /// <summary>
    /// 最后一行上移
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string FinalLineToUp(this string text)
    {
        string[] strPara = Regex.Split(text, @"\r\n");
        if (strPara.Length > 2)
        {
            int n = strPara.Length;
            strPara[n - 2] = strPara[n - 2] + " " + strPara[n - 1];
            strPara[n - 1] = "";
        }
        return string.Join(Environment.NewLine, strPara).Trim();
    }

    /// <summary>
    /// 西语日期格式化
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    /// <exception cref="ArgumentOutOfRangeException"></exception>
    public static string FormatDate_Spanish(this string date)
    {
        if (date.IsNullOrWhiteSpace())
        {
            return string.Empty;
        }

        date = date.Replace(".", "").ToLower();

        string monthWord = date.MatchText(@"(?<=\d+\s*)[A-z]+(?=\s*\d+)").ToLower();

        try
        {
            date = monthWord switch
            {
                "enero" => date.Replace("enero", "jan"),
                "eno" => date.Replace("eno", "jan"),

                "febrero" => date.Replace("febrero", "feb"),
                "fbro" => date.Replace("fbro", "feb"),

                "marzo" => date.Replace("marzo", "mar"),
                "mzo" => date.Replace("mzo", "mar"),

                "abril" => date.Replace("abril", "apr"),
                "ab" => date.Replace("ab", "apr"),

                "mayo" => date.Replace("mayo", "may"),

                "junio" => date.Replace("junio", "jun"),

                "julio" => date.Replace("julio", "jul"),

                "agosto" => date.Replace("agosto", "aug"),
                "agto" => date.Replace("agto", "aug"),

                "septiembre" => date.Replace("septiembre", "sep"),
                "sbre" => date.Replace("sbre", "sep"),

                "octubre" => date.Replace("octubre", "oct"),
                "obre" => date.Replace("obre", "oct"),

                "noviembre" => date.Replace("noviembre", "nov"),
                "nbre" => date.Replace("nbre", "nov"),

                "diciembre" => date.Replace("diciembre", "dec"),
                "didembre" => date.Replace("didembre", "dec"),
                "dbre" => date.Replace("dbre", "dec"),
                "dic" => date.Replace("dic", "dec"),
                _ => throw new ArgumentOutOfRangeException(date)
            };
        } 
        catch (ArgumentOutOfRangeException e)
        {
            return date.FormatDate();
        }

        return date.FormatDate();
    }


    #region 欧洲金额格式化

    public static string FormatAmount_EUR(this string amount)
    {
        amount = amount.Replace("€", "").Replace(",", "").Replace(".", "").Replace("EUR", "").Trim();
        return amount.Length > 2 ? amount.Insert(amount.Length - 2, ".") : amount;
    }

    #endregion

    #region 金额格式化

    public static string FormatAmount(this string amount, string fbKey)
    {
        if (Regex.IsMatch(amount, @"[\!\?\-]{1,}|[\.\s]{2,}"))
        {
            return amount;
        }

        amount = amount.Replace("$", "").ReplaceLetter().ReplaceBlank();

        if (Regex.IsMatch(amount, @"[, ]\d{1,2}$|\.\d{3}$", RegexOptions.Compiled)) //欧洲金额, 第一种是有小数且小数点为,或空, 第二种是没有小数但有千分位为点的
        {
            amount = amount.Replace(".", "").Replace(",", ".").Replace(" ", ".").Trim();
        }
        else if (Regex.IsMatch(amount, @"[\,\s]\d{3}$", RegexOptions.Compiled)) //无小数位,且整数部分含有以逗号或空白的千分位,为了避免和上面的小数空白分隔符混淆单开一行
        {
            amount = amount.Replace(",", "").Replace(" ", "");
        }

        if (Regex.IsMatch(amount, @"(-)?([1-9](,?)\d{0,3}(,?)\d{0,3}|0)([.]\d{1,2})?", RegexOptions.Compiled)) //防止todecimal崩溃, 只对全数字的进行decimal
        {
            amount = ParseDecimal(amount).ToString("F");
        }

        return ParseDecimal(amount).ToString("F");
    }

    public static decimal ParseDecimal(string decimalStr, decimal defaultValue = 0)
    {
        decimal parseDecimal;
        if (decimal.TryParse(decimalStr, out parseDecimal))
            return parseDecimal;
        return defaultValue;
    }

    public static decimal ToDecimal(this string decimalStr, decimal defaultValue = 0)
    {
        decimal parseDecimal;
        if (decimal.TryParse(decimalStr, out parseDecimal))
            return parseDecimal;
        return defaultValue;
    }

    /// <summary>
    /// 转为负
    /// </summary>
    /// <param name="amount"></param>
    /// <returns></returns>
    public static string ConvertNegativeSign(this string amount) => string.IsNullOrWhiteSpace(amount) ? string.Empty : $"-{amount}";

    /// <summary>
    /// 百分比转为小数
    /// </summary>
    /// <param name="percentage"></param>
    /// <returns></returns>
    public static string ConvertPercentageToDecimal(this string percentage) => (percentage.ToDouble() / 100).ToString("F2");

    public static string FormatAmount(this string amount)
    {
        if (Regex.IsMatch(amount.Trim(), @"[\.]{2,}", RegexOptions.Compiled))
        {
            amount = amount.ReplaceTextToOtherText(@"\.{2,}", ".");
        }

        amount = amount.Replace("!","").Replace("?","").Replace(",", "").Replace("$", "").ReplaceLetter().ReplaceBlank();

        if (Regex.IsMatch(amount, @"(-)?([1-9](,?)\d{0,3}(,?)\d{0,3}|0)([.]\d{1,2})?", RegexOptions.Compiled)) //防止todecimal崩溃, 只对全数字的进行decimal
        {
            amount = ParseDecimal(amount).ToString("F");
        }

        return ParseDecimal(amount).ToString("F");
    }

    #endregion

    /// <summary>
    /// 去掉控制字符，不可见的控制字符
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string RemoveControlChars(this string s) => Regex.Replace(s, @"[\p{C}]", "", RegexOptions.Compiled).Trim();

    /// <summary>
    /// 去掉字母
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string ReplaceLetter(this string s) => Regex.Replace(s, @"[A-Za-z]+", "", RegexOptions.Compiled).Trim();

    /// <summary>
    /// 去掉指定内容为空
    /// </summary>
    /// <param name="s"></param>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string ReplaceTextToBlank(this string s, string text) => Regex.Replace(s, @text, "", RegexOptions.Compiled).Trim();

    /// <summary>
    /// 去掉指定内容
    /// </summary>
    /// <param name="s"></param>
    /// <param name="text"></param>
    /// <param name="otherText"></param>
    /// <returns></returns>
    public static string ReplaceTextToOtherText(this string s, string text, string otherText) => Regex.Replace(s, @text, otherText, RegexOptions.Compiled).Trim();

    /// <summary>
    /// 匹配日期
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchDate(this string s) => Regex.Match(s, @"(\d{1,2}/\d{1,2}/\d{2,4})|(\d{1,2}\.\d{1,2}\.\d{2,4})|(\d{1,2}-\d{1,2}-\d{2,4})", RegexOptions.Compiled).Value;

    /// <summary>
    /// 是否匹配日期
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static bool IsMatchDate(this string s) => Regex.IsMatch(s, @"(\d{1,2}/\d{1,2}/\d{2,4})|(\d{1,2}\.\d{1,2}\.\d{2,4})|(\d{1,2}-\d{1,2}-\d{2,4})", RegexOptions.Compiled);

    /// <summary>
    /// 匹配CJK汉字
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchCJK(this string s) => Regex.Match(s, @"\p{IsCJKUnifiedIdeographs}+", RegexOptions.Compiled).Value;

    /// <summary>
    /// 匹配大写字母
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchUpperWords(this string s) => Regex.Match(s, @"\p{Lu}+", RegexOptions.Compiled).Value;

    /// <summary>
    /// 匹配小写字母
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchLowerWords(this string s) => Regex.Match(s, @"\p{Ll}+", RegexOptions.Compiled).Value;

    /// <summary>
    /// 匹配重复的捕获组 abc abc 
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchRepeatWords(this string s) => Regex.Match(s, @"\b(?<w>\w+)\s\k<w>\b", RegexOptions.Compiled).Value;

    /// <summary>
    /// 匹配a-z去掉aeiou
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchTakeDownWords(this string s,string takeDownWords) => Regex.Match(s, @$"[{s}-[{takeDownWords}]]+", RegexOptions.Compiled).Value;

    /// <summary>
    /// 增加几月
    /// </summary>
    /// <param name="s"></param>
    /// <param name="count"></param>
    /// <returns></returns>
    public static string AddMonths(this string s, int count) => DateTime.Parse(s).AddMonths(count).ToString("MM/dd/yyyy");


    /// <summary>
    /// 增加几天
    /// </summary>
    /// <param name="s"></param>
    /// <param name="count"></param>
    /// <returns></returns>
    public static string AddDays(this string s, int count) => DateTime.Parse(s).AddDays(count).ToString("MM/dd/yyyy");

    /// <summary>
    /// 匹配数字
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchNumber(this string s) => Regex.Match(s, @"\d+", RegexOptions.Compiled).Value;

    /// <summary>
    /// 匹配数字
    /// </summary>
    /// <param name="s"></param>
    /// <param name="count"></param>
    /// <returns></returns>
    public static string MatchNumber(this string s,int count) => Regex.Match(s, @"\d{"+ count + "}", RegexOptions.Compiled).Value;

    #region 匹配拉丁单词

    /// <summary>
    /// 匹配拉丁单词
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchLatinWord(this string s) => Regex.Match(s, @"[\p{L}\']+", RegexOptions.Compiled).Value;

    #endregion

    /// <summary>
    /// 匹配标点符号
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchPunctuation(this string s) => Regex.Match(s, @"\p{P}", RegexOptions.Compiled).Value;

    /// <summary>
    /// 替换标点符号为其他文字
    /// </summary>
    /// <param name="s"></param>
    /// <param name="otherText"></param>
    /// <returns></returns>
    public static string ReplacePunctuationToOtherText(this string s, string otherText) => Regex.Replace(s, @"\p{P}", otherText, RegexOptions.Compiled);

    /// <summary>
    /// 匹配字母
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchLetter(this string s) => Regex.Match(s, @"[A-z]+", RegexOptions.Compiled).Value;

    /// <summary>
    /// 匹配非英文字母
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchNonEnglishLetter(this string s) => Regex.Match(s, @"[^\x00-\x7F]+", RegexOptions.Compiled).Value;

    /// <summary>
    /// 匹配邮件名
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchEmail(this string s) => Regex.Match(s, @"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}", RegexOptions.Compiled).Value;

    /// <summary>
    /// 匹配从第一位非0内容
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchTextWithoutStartZero(this string s) => Regex.Match(s, @"(?!0).+", RegexOptions.Compiled).Value;

    /// <summary>
    /// 匹配指定字段
    /// </summary>
    /// <param name="s"></param>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string MatchText(this string s, string text) => Regex.Match(s, @text, RegexOptions.Compiled).Value.Trim();

    /// <summary>
    /// 匹配多个字段
    /// </summary>
    /// <param name="s"></param>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string[] MatchMutiText(this string s, string text) => Regex.Matches(s, @text, RegexOptions.Compiled).Select(m => m.Value.Trim()).ToArray();

    /// <summary>
    /// 是否匹配指定字段
    /// </summary>
    /// <param name="s"></param>
    /// <param name="text"></param>
    /// <returns></returns>
    public static bool IsMatchText(this string s, string text) => Regex.IsMatch(s, @text, RegexOptions.Compiled);

    /// <summary>
    /// 匹配指定字段是否需要删除某一行
    /// </summary>
    /// <param name="s"></param>
    /// <param name="matchText"></param>
    /// <param name="lineIndex"></param>
    /// <returns></returns>
    public static string IsMatchTextNeedRemoveThisLine(this string s, string @matchText, int lineIndex)
    {
        var strLine = s.SplitText(@"\s*\r\n\s*");

        lineIndex = lineIndex switch
        {
            -1 => strLine.Length - 1,
            -2 => strLine.Length - 2,
            -3 => strLine.Length - 3,
            _ => lineIndex
        };

        if (strLine.Length > 0 && strLine.Length > lineIndex)
        {
            if (strLine[lineIndex].IsMatchText(matchText))
            {
                strLine[lineIndex] = "";
                strLine = strLine.Where(str => !str.IsNullOrWhiteSpace()).ToArray();
            }
        }

        return string.Join(Environment.NewLine, strLine);
    }

    /// <summary>
    /// 匹配指定字段是否需要删除下一行
    /// </summary>
    /// <param name="s"></param>
    /// <param name="matchText"></param>
    /// <param name="lineIndex"></param>
    /// <returns></returns>
    public static string IsMatchTextNeedRemoveNextLine(this string s, string @matchText, int lineIndex)
    {
        var strLine = s.SplitText(@"\s*\r\n\s*");

        lineIndex = lineIndex switch
        {
            -1 => strLine.Length - 1,
            -2 => strLine.Length - 2,
            -3 => strLine.Length - 3,
            _ => lineIndex
        };

        if (strLine.Length > 0 && strLine.Length > lineIndex + 1)
        {
            if (strLine[lineIndex].IsMatchText(matchText))
            {
                strLine[lineIndex + 1] = "";
                strLine = strLine.Where(str => !str.IsNullOrWhiteSpace()).ToArray();
            }
        }

        return string.Join(Environment.NewLine, strLine);
    }

    /// <summary>
    /// 是否匹配指定字段
    /// </summary>
    /// <param name="s"></param>
    /// <param name="text"></param>
    /// <returns></returns>
    public static bool IsMatchTextIgnoreCase(this string s, string text) => Regex.IsMatch(s, @text, RegexOptions.IgnoreCase);
    public static bool ContainsIgnoreCase(this string s, string text) => s.Contains(text, StringComparison.OrdinalIgnoreCase);

    /// <summary>
    /// 匹配金额 小数点2位
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchAmount(this string s) => Regex.Match(s, @"(-)?([1-9][,.\s]?\d{0,3}[,.]?\d{0,3}|0)([.,:;-]\d{1,2})?", RegexOptions.Compiled).Value;

    /// <summary>
    /// 添加一个默认值
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string DefaultValue(this string s, string value) => string.IsNullOrWhiteSpace(s) ? value : s;

    /// <summary>
    /// 匹配金额 小数点3位
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchAmount_ThreeDecimalPlaces(this string s) => Regex.Match(s, @"(-)?([1-9][,.\s]?\d{0,3}[,.]?\d{0,3}|0)([.,:;-]\d{1,3})?", RegexOptions.Compiled).Value;

    /// <summary>
    /// 匹配金额 小数点最高位
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string MatchAmount_MaxValueDecimalPlaces(this string s, int maxValue) => Regex.Match(s, @"(-)?([1-9][,.\s]?\d{0,3}[,.]?\d{0,"+ maxValue + @"}|0)([.,:;-]\d{1," + maxValue + "})?", RegexOptions.Compiled).Value;

    /// <summary>
    /// 去掉最后一个文本为空
    /// </summary>
    /// <param name="s"></param>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string ReplaceLastWordsToBlank(this string s, string text) => Regex.Replace(s, $"{text}$", "",RegexOptions.Compiled);


    /// <summary>
    /// 根据字段分割
    /// </summary>
    /// <param name="s"></param>
    /// <param name="keyWords"></param>
    /// <returns></returns>
    public static string[] SplitText(this string s, string keyWords) => Regex.Split(s, @keyWords, RegexOptions.Compiled);
    public static string[] SplitToSingleLine(this string s) => string.IsNullOrWhiteSpace(s) ? [] : s.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

    /// <summary>
    /// 是否匹配金额
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static bool IsMatchAmount(this string s) => Regex.IsMatch(s, @"(-)?([1-9][,.\s]?\d{0,3}[,.]?\d{0,3}|0)([.,:;-]\d{1,3})?", RegexOptions.Compiled);

    /// <summary>
    /// 判断内容为空或者Null
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static bool IsNullOrWhiteSpace(this string s) => string.IsNullOrWhiteSpace(s);

    /// <summary>
    /// 获取变量名称
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="memberExpression"></param>
    /// <returns></returns>
    public static string GetMemberName<T>(Expression<Func<T>> memberExpression)
    {
        MemberExpression expressionBody = (MemberExpression)memberExpression.Body;
        return expressionBody.Member.Name;
    }

    /// <summary>
    /// 转换特殊时间 ex:22-02-22
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    public static string FormatSpecialDateTime(this string date)
    {
        if (date.Contains("-"))
        {
            SwapDate(@"-");
        }
        else if (date.Contains("."))
        {
            SwapDate(@"\.");
        }
        else if (date.Contains("/"))
        {
            SwapDate(@"\/");
        }

        void SwapDate(string label)
        {
            var strDate = Regex.Split(date, label);
            if (strDate.Length == 3)
            {
                (strDate[0], strDate[1]) = (strDate[1], strDate[0]);
            }
            date = string.Join("-", strDate);
        }

        return date.FormatDate();
    }

    #region 累加每行数字

    /// <summary>
    /// 累加每行数字
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string CountLineAmount(this string text)
        => text.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
            .Select(s => s.MatchAmount().ToDecimal())
            .Sum().ToString("F2");

    /// <summary>
    /// 累加每行数字
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string CumulationRowNumber(string text)
    {
        float result = 0;

        if (!string.IsNullOrWhiteSpace(text))
        {
            string[] arrVals = Regex.Split(text, @"\s*\r\n\s*");
            arrVals = arrVals.Select(str => Regex.Match(str, @"(-)?([1-9][,.\s]?\d{0,3}[,.]?\d{0,3}|0)([.,:;-]\d{1,2})?").Value)
                             .Where(str => !string.IsNullOrWhiteSpace(str.Trim())).ToArray();

            for (int i = 0; i < arrVals.Length; i++)
            {
                result += float.Parse(arrVals[i].FormatAmount());
            }
        }

        string sResult = result.ToString();
        if (sResult.Contains("."))
        {
            int decimals = sResult.Length - sResult.IndexOf(".", StringComparison.Ordinal) - 1;
            if (decimals == 3)
            {
                return $"{result:N3}";
            }
        }

        if (!result.ToString("F").Contains("."))
        {
            return $"{result:N0}";
        }

        return $"{result:N2}";

    }

    #endregion

    #region 计算字符串里有多少个数标点符号返回int
    /// <summary>
    /// 计算字符串里有多少个数标点符号返回int
    /// </summary>
    /// <param name="str"></param>
    /// <param name="needToCountString"></param>
    /// <returns></returns>
    public static int CountThisPunctuation(this string str, string needToCountString)
    {
        int count = 0;
        if (str.Contains(needToCountString))
        {
            string strReplaced = str.Replace(needToCountString, "");
            count = (str.Length - strReplaced.Length) / needToCountString.Length;
        }

        return count;
    }

    #endregion

    #region 链式转换

    private delegate bool TryParseDelegate<T>(string s, out T result);

    private static T To<T>(string value, TryParseDelegate<T> parse)
        => (parse(value, out T result) ? result : default)!;

    public static int ToInt32(this string value)
        => To<int>(value, int.TryParse);

    public static double ToDouble(this string value)
        => To<double>(value, double.TryParse);

    public static DateTime ToDateTime(this string value)
        => To<DateTime>(value, DateTime.TryParse);

    public static IPAddress ToIPAddress(this string value)
        => To<IPAddress>(value, IPAddress.TryParse!);

    public static TimeSpan ToTimeSpan(this string value)
        => To<TimeSpan>(value, TimeSpan.TryParse);


    #endregion

    #region 根据位数前面补0

    public static string ZeroPaddingBaseOnLength(this string text, int length) => !text.IsNullOrWhiteSpace() && text.Length < length ? text.PadLeft(length, '0') : text;

    #endregion

    #region 尝试取逗号之前的内容

    public static string TryGetStringBeforeComma(this string text) => text.Contains(",") ? text[..text.IndexOf(',')]: text;

    #endregion

    #region KG to LBS
    public static string ConvertKG_to_LBS(this string kilograms) => (kilograms.MatchAmount().FormatAmount().ToDouble() * 2.20462262185).ToString("F0");

    #endregion

    /// <summary>
    /// 获取文本的第一个是首字母的单词
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string ReturnFirstLetteredWord(this string text)
    {
        var strLines = text.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
        if (strLines.Length > 0)
        {
            var strWords = strLines[0].Split(new[] { ' ', '-' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
            for (int i = 0; i < strWords.Length; i++)
            {
                if (char.IsLetter(strWords[i][0]))
                {
                    return strWords[i];
                }
            }
        }

        return string.Empty;
    }
    /// <summary>
    /// 获取地址的第一个单词
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string ReturnFirstWord(this string text)
    {
        var strLines = text.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
        if (strLines.Length > 0)
        {
            var strWords = strLines[0].Split(new[] { ' ', '-' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
            if (strWords.Length > 0)
            {
                return strWords[0];
            }
        }

        return string.Empty;
    }

    /// <summary>
    /// 获取地址的前两个单词
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string ReturnFirstAndSecondWord(this string text)
    {
        var strLines = text.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
        if (strLines.Length > 0)
        {
            var strWords = strLines[0].Split([' ', '-'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
            if (strWords.Length > 1)
            {
                return strWords[0] + " " + strWords[1];
            }
        }

        return string.Empty;
    }

    /// <summary>
    /// 获取文本的第二行
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string GetSecondLineText(this string text)
    => text.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
           .ElementAtOrDefault(1) ?? text;

    public static string DefaultEmptyWhenEqualValue(this string s, string value) => s.Equals(value) ? "" : s;

    public static string AddUpOrDown(this decimal d) => d > 0 ? $"上升{d}" : $"下降{d.ToString(CultureInfo.InvariantCulture).Replace("-","")}";
}

